package com.have.blog.web.controller;

import com.have.blog.base.exception.ThrowableUtils;
import com.have.blog.base.validator.group.Insert;
import com.have.blog.base.validator.group.Update;
import com.have.blog.base.wrapper.Wrapper;
import com.have.blog.config.hloder.RequestHolder;
import com.have.blog.utils.StringUtils;
import com.have.blog.web.dto.FeedbackDto;
import com.have.blog.web.dto.UpdatePwdDto;
import com.have.blog.web.dto.UserDto;
import com.have.blog.web.service.FeedbackService;
import com.have.blog.web.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RefreshScope
@RequestMapping("/oauth")
@Api(value = "第三方登录相关接口", tags = {"第三方登录相关接口"})
@Slf4j
public class AuthController {
    @Autowired
    private FeedbackService feedbackService;
    @Autowired
    private UserService userService;

    @ApiOperation(value = "获取用户信息", notes = "获取用户信息")
    @GetMapping("/verify/{accessToken}")
    public Wrapper verifyUser(@PathVariable("accessToken") String accessToken) {
        Integer userUid = RequestHolder.getUserUid();
        UserDto dto = userService.getUserByUid(userUid);
        return Wrapper.success(dto);
    }

    @ApiOperation(value = "获取用户反馈", notes = "获取用户反馈")
    @GetMapping("/getFeedbackList")
    public Wrapper getFeedbackList() {
        List<FeedbackDto> resultList = feedbackService.getUserFeedbackList();
        return Wrapper.success(resultList);
    }

    @ApiOperation(value = "提交反馈", notes = "提交反馈", response = String.class)
    @PostMapping("/addFeedback")
    public Wrapper edit(HttpServletRequest request, @Validated({Insert.class}) @RequestBody FeedbackDto feedbackDto, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);

        return feedbackService.addFeedback(feedbackDto);
    }

    @ApiOperation(value = "编辑用户", notes = "编辑用户", response = String.class)
    @PostMapping("/editUser")
    public Wrapper editUser(@RequestBody UserDto userDto) {
        return userService.editUser(userDto);
    }

    @ApiOperation(value = "更新密码", notes = "更新密码", response = String.class)
    @PostMapping("/updateUserPwd")
    public Wrapper updatePwd(@RequestParam(value = "oldPwd") String oldPwd, @RequestParam("newPwd") String newPwd) {
        if (StringUtils.isEmpty(oldPwd) || StringUtils.isEmpty(newPwd)) {
            return Wrapper.failed();
        }
        UpdatePwdDto dto = new UpdatePwdDto();
        dto.setNewPwd(newPwd);
        dto.setOldPwd(oldPwd);
        return userService.updatePwd(dto);
    }

    @ApiOperation(value = "用户登出", notes = "用户登出", response = String.class)
    @PostMapping("/delete/{token}")
    public Wrapper logout(@PathVariable(value = "token") String token) {
        System.out.println(token);
        return userService.logout(token);
    }
}
